package com.weetop.aklyscan.activity;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.classic.adapter.BaseAdapterHelper;
import com.classic.adapter.CommonRecyclerAdapter;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnSelectListener;
import com.qmuiteam.qmui.layout.QMUIButton;
import com.weetop.aklyscan.R;
import com.weetop.aklyscan.appConfig.MyApplication;
import com.weetop.aklyscan.base.BaseFragment;
import com.weetop.aklyscan.base.BaseUrl;
import com.weetop.aklyscan.base.mvp.BaseModel;
import com.weetop.aklyscan.bean.ChaXunBean;
import com.weetop.aklyscan.bean.ChanpinChukuBean;
import com.weetop.aklyscan.bean.InStockItemBean;
import com.weetop.aklyscan.bean.ProductInfo;
import com.weetop.aklyscan.bean.TransferBoxBean;
import com.weetop.aklyscan.bean.TypeInfo;
import com.weetop.aklyscan.bean.ZXGoodsInfo;
import com.weetop.aklyscan.bean.ZXOrderInfo;
import com.weetop.aklyscan.presenter.ZxProductPresent;
import com.weetop.aklyscan.utils.BaseUtils;
import com.weetop.aklyscan.utils.widget.MyDialogUtil;
import com.weetop.aklyscan.view.ZxProductView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cc.shinichi.library.tool.utility.ui.ToastUtil;

public class Fragment_zx_op_product1 extends BaseFragment<ZxProductPresent> implements ZxProductView {
    private static final int HUOJIA_CODE = 12000;
    private static final int REQUEST_HUOJIA_CODE_SCAN_ONE = 22000;

    @BindView(R.id.rcy_product)
    RecyclerView rcyProduct;
    @BindView(R.id.rcy_data)
    RecyclerView rcyData;
    @BindView(R.id.rcy_chuku)
    RecyclerView rcyChuku;
    Unbinder unbinder;
    @BindView(R.id.et_zzx_code)
    TextView etZzxCode;
    @BindView(R.id.et_zzx_code1)
    TextView etZzxCode1;
    @BindView(R.id.btn_save)
    QMUIButton btnSave;
    @BindView(R.id.qingkong)
    TextView qingkong;
    @BindView(R.id.bianma)
    TextView bianma;
    @BindView(R.id.caozuoType)
    TextView caozuoType;
    @BindView(R.id.type)
    TextView type;
    @BindView(R.id.tv_order_code)
    TextView tv_order_code;
    @BindView(R.id.bianhao_ll)
    LinearLayout bianhao_ll;
    @BindView(R.id.list_ll)
    LinearLayout list_ll;
    @BindView(R.id.chuku_ll)
    LinearLayout chuku_ll;
    @BindView(R.id.button)
    TextView button;
    @BindView(R.id.tv_totle_postion)
    TextView totlePostion;
    @BindView(R.id.dataNum)
    TextView dataNum;
    @BindView(R.id.scanType_ll)
    LinearLayout scanType_ll;
    @BindView(R.id.list1_ll)
    LinearLayout list1_ll;
    private CommonRecyclerAdapter<InStockItemBean> adapter;
    private CommonRecyclerAdapter<ZXOrderInfo> adapter2;
    private CommonRecyclerAdapter<ChanpinChukuBean> adapter1;
    private List<InStockItemBean> list = new ArrayList<>();
    private List<ChanpinChukuBean> chuku_list = new ArrayList<>();
    private List<ZXOrderInfo> productISList;
    private int alreadyInStockCount;
    private String barcode, orderID, xiangCode;
    boolean bSave1 = true;
    boolean bSave2 = true;

    @Override
    protected ZxProductPresent createPresenter() {
        return new ZxProductPresent(this, mActivity);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_zx_product1, null);
        unbinder = ButterKnife.bind(this, view);
        rcyProduct.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter = new CommonRecyclerAdapter<InStockItemBean>(mActivity, R.layout.item_product, list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, InStockItemBean item, int position) {
                helper.setText(R.id.tv_id, position + 1 + "");
                helper.setText(R.id.tv_code, item.getGoods_code());
                String product_name = TextUtils.isEmpty(item.getGoods_name()) ? "-" : item.getGoods_name();
                String product_model = TextUtils.isEmpty(item.getGoods_model()) ? "-" : item.getGoods_model();
                helper.setText(R.id.tv_name, product_name);
                helper.setText(R.id.tv_model, product_model);
                helper.setVisible(R.id.btn_remove, View.VISIBLE);
                helper.setOnClickListener(R.id.btn_remove, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BaseUtils.fastClick()) {
                            if (list.size() > position) {
                                list.remove(position);
                                adapter.remove(position);
                            }
                        }
                    }
                });
//                } else {
//                    helper.setVisible(R.id.btn_remove, View.INVISIBLE);
//                }
            }
        };
        rcyProduct.setAdapter(adapter);

        rcyChuku.setLayoutManager(new LinearLayoutManager(mActivity));
        adapter1 = new CommonRecyclerAdapter<ChanpinChukuBean>(mActivity, R.layout.item_chanpin_chuku, chuku_list) {
            @Override
            public void onUpdate(BaseAdapterHelper helper, ChanpinChukuBean item, int position) {
                helper.setText(R.id.tv_id, position + 1 + "");
                helper.setText(R.id.tv_code, item.getCode());
                helper.setText(R.id.tv_name, item.getGoods_name());
                helper.setText(R.id.tv_model, item.getGoods_model());
                helper.setText(R.id.tv_bianhao, item.getCase_code());
                helper.setVisible(R.id.btn_remove, View.VISIBLE);
                if (item.isError()) {
                    helper.setBackgroundRes(R.id.ll, R.drawable.tab_line_error);
                } else {
                    helper.setBackgroundRes(R.id.ll, R.drawable.tab_line);
                }
                helper.setOnClickListener(R.id.btn_remove, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (BaseUtils.fastClick()) {
                            if (chuku_list.size() > position) {
                                chuku_list.remove(position);
                                adapter1.remove(position);
                            }
                        }
                    }
                });
//                } else {
//                    helper.setVisible(R.id.btn_remove, View.INVISIBLE);
//                }
            }
        };
        rcyChuku.setAdapter(adapter1);

        caozuoType.setTag(0);
        type.setTag(0);
//        onBarcodeEvent("CPCG202106250001");
        return view;
    }

    @Override
    public void getSearchResultSuccess(BaseModel<TransferBoxBean> o) {
        TransferBoxBean data = o.getData();
        barcode = data.getBarcode();
        etZzxCode.setText(barcode);
//        etCkName.setText(data.getTier_name());
//        etCkPostion.setText(data.getTier_code());
        List<TransferBoxBean.ItemsBean> items = data.getItems();
        list.clear();
        if (items != null && items.size() > 0) {
            for (int i = 0; i < items.size(); i++) {
                TransferBoxBean.ItemsBean item = items.get(i);
                //产品类型：1产品；2配件
                if (item.getProduct_type() == 1) {
                    InStockItemBean goodsItems = new InStockItemBean();
                    goodsItems.setGoods_code(item.getProduct_code());
                    goodsItems.setGoods_name(item.getProduct_name());
                    goodsItems.setGoods_type(item.getProduct_type());
                    goodsItems.setCount(item.getNumber());
                    //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
                    goodsItems.setStatus(1);
                    list.add(goodsItems);
                }
            }
        }
        alreadyInStockCount = list.size();
        adapter.replaceAll(list);
    }

    @Override
    public void getProductInfSuccess(BaseModel<ProductInfo> o) {
        ProductInfo data = o.getData();
        InStockItemBean goodsItems = new InStockItemBean();
        goodsItems.setGoods_code(data.getGoods_sn());
        goodsItems.setGoods_name(data.getGoods_name());
        //产品类型：1产品；2配件
        goodsItems.setGoods_type(1);
        goodsItems.setCount(1);
        //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
        goodsItems.setStatus(0);
        list.add(goodsItems);
        adapter.add(goodsItems);
    }

    @Override
    public void saveZxSuccess(BaseModel<Object> o) {
//        mPresenter.searchList(boxCode);
//        ToastUtils.showShort("保存成功");
    }

    @Override
    public void getInto08Success(BaseModel<ZXGoodsInfo> o) {
        ZXGoodsInfo data = o.getData();
        InStockItemBean goodsItems = new InStockItemBean();
        goodsItems.setGoods_code(data.getSerial_code());
        goodsItems.setGoods_name(data.getGoods_name());
        goodsItems.setGoods_model(data.getGoods_model());
        goodsItems.setAudio_url(data.getAudio_url());
        goodsItems.setCount(1);
        goodsItems.setStatus(data.getStatus());
        if (data.getStatus() == 1) {
            ToastUtil.getInstance()._short(getActivity(), "产品已入库");
//            playUrl(goodsItems.getAudio_url(), 0);
            playUrl("", 0);
//            playRing("wuxiao.mp3");
            return;
        }
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getGoods_code().equals(goodsItems.getGoods_code())) {
                MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//                playUrl(goodsItems.getAudio_url(), 0);
                playUrl("", 0);
//                playRing("wuxiao.mp3");
                return;
            }
        }
        if (((int) type.getTag()) == 0) {
            if (list.size() != 0 && !list.get(0).getGoods_model().equals(goodsItems.getGoods_model())) {
                MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
//                playUrl(goodsItems.getAudio_url(), 0);
                playUrl("", 0);
                return;
            }
        }
        list.add(goodsItems);
        adapter.add(goodsItems);
        rcyProduct.smoothScrollToPosition(list.size());
        playUrl(goodsItems.getAudio_url(), 1);
        dataNum.setText("产品数量：" + list.size());
    }

    @Override
    public void getInto09Success(BaseModel<Object> o) {
        ToastUtil.getInstance()._short(getActivity(), "操作成功");

        list.clear();
        adapter.clear();
        adapter.notifyDataSetChanged();
        etZzxCode.setText("");
    }

    @Override
    public void getInto12Success(BaseModel<List<ZXOrderInfo>> o) {

    }

    @Override
    public void getInto32Success(BaseModel<List<ZXOrderInfo>> o) {
        productISList = o.getData();
        int p = 0;
        for (int i = 0; i < productISList.size(); i++) {
            p = p + productISList.get(i).getCount();
        }
        totlePostion.setText(p + "");
        if (productISList != null && productISList.size() > 0) {
            rcyData.setLayoutManager(new LinearLayoutManager(mActivity));
            adapter2 = new CommonRecyclerAdapter<ZXOrderInfo>(mActivity, R.layout.item_scanout1, productISList) {
                @Override
                public void onUpdate(BaseAdapterHelper helper, ZXOrderInfo item, int position) {
                    helper.setText(R.id.tv_xuhao, position + 1 + "");
                    helper.setText(R.id.tv_huopin_xinghao, item.getGoods_model());
                    helper.setText(R.id.tv_huopin_mingcheng, item.getGoods_name());
                    helper.setText(R.id.tv_yingru_zongliang, item.getYc_num() + "");
                    helper.setText(R.id.et_weiruzongliang, item.getWc_num() + "");
                    helper.setText(R.id.tv_huopin_dindan, item.getNum() + "");
                }
            };
            rcyData.setAdapter(adapter2);
            playRing("shujuyouxiao.mp3");
        } else {
            adapter2.clear();
            adapter2.notifyDataSetChanged();
            playRing("shujuwuxiao.mp3");
        }
        tv_order_code.setBackgroundResource(R.drawable.et_grey);
        if (TextUtils.isEmpty(etZzxCode.getText().toString().trim())) {
            etZzxCode.setBackgroundResource(R.drawable.et_yellow);
        } else {
            etZzxCode.setBackgroundResource(R.drawable.et_grey);
        }
        if (TextUtils.isEmpty(etZzxCode1.getText().toString().trim())) {
            etZzxCode1.setBackgroundResource(R.drawable.et_yellow);
        } else {
            etZzxCode1.setBackgroundResource(R.drawable.et_grey);
        }
    }

    @Override
    public void getInto26Success(BaseModel<List<TypeInfo>> o) {
    }

    @Override
    public void getInto19Success(BaseModel<Object> o, String code) {
        mPresenter.Into08("1", code, tv_order_code.getText().toString().trim());
    }

    @Override
    public void getInto20Success(BaseModel<String> o) {
        bSave1 = true;
        playRing("zhuangxiangwancheng.mp3");
        ToastUtil.getInstance()._short(getActivity(), "操作成功");
        List<ModelBean> modelList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            final int a = i;
            if (!modelList.stream().filter(w -> String.valueOf(w.getModel()).equals(list.get(a).getGoods_model())).findAny().isPresent()) {
                ModelBean modelBean = new ModelBean();
                modelBean.setModel(list.get(i).getGoods_model());
                modelBean.setNum(0);
                modelList.add(modelBean);
            }
        }
        for (int i = 0; i < modelList.size(); i++) {
            for (int j = 0; j < list.size(); j++) {
                if (modelList.get(i).getModel().equals(list.get(j).getGoods_model())) {
                    modelList.get(i).setNum(modelList.get(i).getNum() + 1);
                }
            }
        }
        mPresenter.Into32(tv_order_code.getText().toString().trim());
        try {
            JSONArray jsonArray = new JSONArray();
            JSONObject tmpObj = null;
            for (int i = 0; i < modelList.size(); i++) {
                tmpObj = new JSONObject();
                tmpObj.put("goods_model", modelList.get(i).getModel());
                tmpObj.put("new_num", modelList.get(i).getNum());
                jsonArray.put(tmpObj);
                tmpObj = null;
            }
            mPresenter.Into36(jsonArray, tv_order_code.getText().toString().trim());
        } catch (Exception e) {
            e.printStackTrace();
        }
        list.clear();
        adapter.clear();
        adapter.notifyDataSetChanged();

    }

    @Override
    public void getInto21Success(BaseModel<String> o) {
        bSave2 = true;
        playRing("rukuwancheng.mp3");
        String data = o.getData();
        if (!TextUtils.isEmpty(data)) {
            List<String> errorList = Arrays.asList(data.split(","));
            for (int j = 0; j < chuku_list.size(); j++) {
                chuku_list.get(j).setError(false);
                for (int i = 0; i < errorList.size(); i++) {
                    if (errorList.get(i).equals(chuku_list.get(j).getCode())) {
                        chuku_list.get(j).setError(true);
                    }
                }
            }
            adapter1.notifyDataSetChanged();
        } else {
            ToastUtil.getInstance()._short(getActivity(), "操作成功");
            List<ModelBean> modelList = new ArrayList<>();
            for (int i = 0; i < chuku_list.size(); i++) {
                final int a = i;
                if (!modelList.stream().filter(w -> String.valueOf(w.getModel()).equals(chuku_list.get(a).getGoods_model())).findAny().isPresent()) {
                    ModelBean modelBean = new ModelBean();
                    modelBean.setModel(chuku_list.get(i).getGoods_model());
                    modelBean.setNum(0);
                    modelList.add(modelBean);
                }
            }
            for (int i = 0; i < modelList.size(); i++) {
                for (int j = 0; j < chuku_list.size(); j++) {
                    if (modelList.get(i).getModel().equals(chuku_list.get(j).getGoods_model())) {
                        modelList.get(i).setNum(modelList.get(i).getNum() + 1);
                    }
                }
            }
            mPresenter.Into32(tv_order_code.getText().toString().trim());
            try {
                JSONArray jsonArray = new JSONArray();
                JSONObject tmpObj = null;
                for (int i = 0; i < modelList.size(); i++) {
                    tmpObj = new JSONObject();
                    tmpObj.put("goods_model", modelList.get(i).getModel());
                    tmpObj.put("new_num", modelList.get(i).getNum());
                    jsonArray.put(tmpObj);
                    tmpObj = null;
                }
                mPresenter.Into31(jsonArray, tv_order_code.getText().toString().trim());
            } catch (Exception e) {
                e.printStackTrace();
            }
            chuku_list.clear();
            adapter1.clear();
            adapter1.notifyDataSetChanged();
            playRing("chukuwancheng.mp3");
        }
    }

    @Override
    public void getInto22Success(BaseModel<List<ChanpinChukuBean>> o) {
        List<ChanpinChukuBean> data = o.getData();
        List<ChanpinChukuBean> list14 = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {
            ChanpinChukuBean goodsItems = new ChanpinChukuBean();
            goodsItems.setCase_code(data.get(i).getCase_code());
            goodsItems.setCode(data.get(i).getCode());
            goodsItems.setGoods_model(data.get(i).getGoods_model());
            goodsItems.setGoods_name(data.get(i).getGoods_name());
            goodsItems.setOrder_sn(data.get(i).getOrder_sn());
            //状态:-1已作废；0未入库；1在库里；2未出库；3已出库
            goodsItems.setStatus(data.get(i).getStatus());
            list14.add(goodsItems);
        }
        if (list14.size() < 1) {
            playRing("shujuwuxiao.mp3");
            return;
        }
        if (!tv_order_code.getText().toString().trim().equals(list14.get(0).getOrder_sn())) {
            System.out.println(tv_order_code.getText().toString().trim());
            System.out.println(list14.get(0).getOrder_sn());
            playRing("shujuwuxiao.mp3");
            return;
        }
        chuku_list.addAll(list14);
        adapter1.addAll(list14);
        etZzxCode1.setText(xiangCode);
        etZzxCode1.setBackgroundResource(R.drawable.et_grey);
        playRing("luruchenggong.mp3");
        dataNum.setText("产品数量：" + chuku_list.size());
        mPresenter.Into32(tv_order_code.getText().toString().trim());
    }

    @Override
    public void getInto25Success(BaseModel<Object> o) {
        tv_order_code.setText(orderID);
        tv_order_code.setBackgroundResource(R.drawable.et_grey);
        mPresenter.Into32(tv_order_code.getText().toString().trim());
    }

    @Override
    public void getInto31Success(BaseModel<List<ChaXunBean>> o) {
        List<ChaXunBean> chaXunBeans = o.getData();
        for (int i = 0; i < chaXunBeans.size(); i++) {
            TiJiaoDialog tiJiaoDialog = new TiJiaoDialog(getActivity(), chaXunBeans.get(i), chaXunBeans.size(), chaXunBeans.size() - i, 0);
            tiJiaoDialog.show();
        }
    }

    @Override
    public void getInto36Success(BaseModel<List<ChaXunBean>> o) {
        List<ChaXunBean> chaXunBeans = o.getData();
        for (int i = 0; i < chaXunBeans.size(); i++) {
            TiJiaoDialog tiJiaoDialog = new TiJiaoDialog(getActivity(), chaXunBeans.get(i), chaXunBeans.size(), chaXunBeans.size() - i, 2);
            tiJiaoDialog.show();
        }
    }

    @Override
    public void getInto33Success(BaseModel<String> o) {
        etZzxCode.setText(barcode);
        etZzxCode.setBackgroundResource(R.drawable.et_grey);
        playRing("shujuyouxiao.mp3");
    }

    @Override
    public void onDataError() {
        playRing("shujuwuxiao.mp3");
    }

    @Override
    public void onError() {
        MyDialogUtil.showDialog(getActivity(), "数据无效已删除");
        playRing("wuxiao.mp3");
    }

    @Override
    public void onUpFail(String mag) {
        bSave1 = true;
        bSave2 = true;
        MyDialogUtil.showDialog(getActivity(), mag);
        playRing("wuxiao.mp3");
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.btn_save, R.id.qingkong, R.id.qingkong1, R.id.qingkong11, R.id.caozuoType, R.id.type, R.id.button, R.id.jilu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_save:
                try {
                    if (BaseUtils.fastClick()) {
                        if (((int) caozuoType.getTag()) == 0) {
                            if (!bSave1) {
                                return;
                            }
                            if (TextUtils.isEmpty(tv_order_code.getText().toString().trim())) {
                                ToastUtil.getInstance()._short(getActivity(), "订单编号不能为空");
                                return;
                            }
                            if (TextUtils.isEmpty(etZzxCode.getText().toString().trim())) {
                                ToastUtil.getInstance()._short(getActivity(), "外箱编号不能为空");
                                return;
                            }
                            bSave1 = false;
                            JSONArray jsonArray = new JSONArray();
                            JSONObject tmpObj = null;
                            for (int i = 0; i < list.size(); i++) {
                                tmpObj = new JSONObject();
                                tmpObj.put("order_type", "1");
                                tmpObj.put("order_sn", tv_order_code.getText().toString().trim());
                                tmpObj.put("code", list.get(i).getGoods_code());
                                tmpObj.put("num", "1");
                                tmpObj.put("case_code", etZzxCode.getText().toString().trim());
                                tmpObj.put("goods_name", list.get(i).getGoods_name());
                                tmpObj.put("goods_model", list.get(i).getGoods_model());
                                jsonArray.put(tmpObj);
                                tmpObj = null;
                            }
                            if (list.size() > 0) {
                                mPresenter.Into20(jsonArray);
                            } else {
                                bSave1 = true;
                                ToastUtils.showShort("无新增产品");
                            }
                        } else {
                            if (!bSave2) {
                                return;
                            }
                            if (TextUtils.isEmpty(tv_order_code.getText().toString().trim())) {
                                ToastUtil.getInstance()._short(getActivity(), "订单编号不能为空");
                                return;
                            }
                            bSave2 = false;
                            JSONArray jsonArray = new JSONArray();
                            JSONObject tmpObj = null;
                            for (int i = 0; i < chuku_list.size(); i++) {
                                tmpObj = new JSONObject();
                                tmpObj.put("case_code", chuku_list.get(i).getCase_code());
                                tmpObj.put("code", chuku_list.get(i).getCode());
                                jsonArray.put(tmpObj);
                                tmpObj = null;
                            }
                            if (chuku_list.size() > 0) {
                                mPresenter.Into21(jsonArray, tv_order_code.getText().toString().trim());
                            } else {
                                bSave2 = true;
                                ToastUtils.showShort("无新增产品");
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.qingkong:
                tv_order_code.setText("");
                playRing("qingkong.mp3");
                tv_order_code.setBackgroundResource(R.drawable.et_yellow);
                break;
            case R.id.qingkong1:
                etZzxCode.setText("");
                playRing("qingkong.mp3");
                if (!TextUtils.isEmpty(tv_order_code.getText().toString().trim())) {
                    etZzxCode.setBackgroundResource(R.drawable.et_yellow);
                }
                break;
            case R.id.qingkong11:

                etZzxCode1.setText("");
                playRing("qingkong.mp3");
                if (!TextUtils.isEmpty(tv_order_code.getText().toString().trim())) {
                    etZzxCode1.setBackgroundResource(R.drawable.et_yellow);
                }
                break;
            case R.id.caozuoType:
                XPopup.get(getActivity()).asCenterList("请选择类型", new String[]{"装箱", "出库"},
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                caozuoType.setText(text);
                                caozuoType.setTag(position);
                                if (position == 0) {
                                    list_ll.setVisibility(View.VISIBLE);
                                    chuku_ll.setVisibility(View.GONE);
                                    bianhao_ll.setVisibility(View.VISIBLE);
                                    scanType_ll.setVisibility(View.VISIBLE);
                                    list1_ll.setVisibility(View.GONE);
                                    btnSave.setText("装箱保存");
                                } else {
                                    list_ll.setVisibility(View.GONE);
                                    chuku_ll.setVisibility(View.VISIBLE);
                                    bianhao_ll.setVisibility(View.GONE);
                                    scanType_ll.setVisibility(View.GONE);
                                    list1_ll.setVisibility(View.VISIBLE);
                                    btnSave.setText("出库保存");
//                                    onBarcodeEvent("CP00000221");
                                }
                            }
                        })
                        .show();
                break;
            case R.id.button:
                mPresenter.Into25("1", tv_order_code.getText().toString().trim());
                break;
            case R.id.type:
                XPopup.get(getActivity()).asCenterList("请选择类型", new String[]{"同型号", "异型号"},
                        // null, /** 图标Id数组，可无 **/
                        // 1,    /** 选中的position，默认没有选中效果 **/
                        new OnSelectListener() {
                            @Override
                            public void onSelect(int position, String text) {
                                type.setTag(position);
                                type.setText(text);
                            }
                        })
                        .show();
                break;
            case R.id.jilu:
                if (TextUtils.isEmpty(tv_order_code.getText().toString())) {
                    ToastUtil.getInstance()._short(getActivity(), "订单编号不能为空");
                    return;
                }
                if (((int) caozuoType.getTag()) == 0) {
                    Intent intent = new Intent(getActivity(), ZXRecordListActivity.class);
                    intent.putExtra("order_sn", tv_order_code.getText().toString().trim());
                    getActivity().startActivity(intent);
                } else {
                    Intent intent = new Intent(getActivity(), OutRecordListActivity.class);
                    intent.putExtra("order_type", OutRecordListActivity.ORDER_TYPE_PRODUCT_SC);
                    intent.putExtra("order_sn", tv_order_code.getText().toString().trim());
                    getActivity().startActivity(intent);
                }
                break;
            default:
                throw new IllegalStateException("Unexpected value: " + view.getId());
        }
    }

    public void onBarcodeEvent(String code) {
        System.out.println("扫描结果1：" + code);
        if (TextUtils.isEmpty(tv_order_code.getText().toString().trim())) {
            orderID = code;
            mPresenter.Into25("1", code);
            return;
        }
        if (((int) caozuoType.getTag()) == 0) {
            if (TextUtils.isEmpty(etZzxCode.getText().toString().trim())) {
                barcode = code;
                mPresenter.Into33(code);
                return;
            }
            if (TextUtils.isEmpty(caozuoType.getText().toString().trim())) {
                ToastUtil.getInstance()._short(getActivity(), "操作类型不能为空");
                return;
            }
            if (TextUtils.isEmpty(tv_order_code.getText().toString().trim())) {
                ToastUtil.getInstance()._short(getActivity(), "订单编号不能为空");
                return;
            }
            mPresenter.Into19("1", tv_order_code.getText().toString().trim(), code, "1");
        } else {
            if (TextUtils.isEmpty(etZzxCode1.getText().toString().trim())) {
                xiangCode = code;
                mPresenter.Into22(code);
            }
        }
    }

    private AssetManager assetManager;
    private MediaPlayer player = new MediaPlayer();
    private boolean isPlay;

    private void playRing(String fileName) {
        try {
            isPlay = false;
            assetManager = getActivity().getAssets();
            AssetFileDescriptor fileDescriptor = assetManager.openFd(fileName);
//            if (player.isPlaying()) {
            player.reset();
//            }
            player.setDataSource(fileDescriptor.getFileDescriptor(), fileDescriptor.getStartOffset(),
                    fileDescriptor.getLength());
            player.prepare();
            player.start();
            player.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                @Override
                public void onSeekComplete(MediaPlayer mp) {
                    player.stop();
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void playUrl(String fileName, final int i) {
        if (TextUtils.isEmpty(fileName) || fileName.equals("null")) {
            if (i == 1) {
                playRing("luruchenggong.mp3");
                return;
            } else {
                playRing("wuxiao.mp3");
                return;
            }
        }
        try {
            isPlay = true;
            player.reset();
            if (!fileName.contains("http")) {
                player.setDataSource(BaseUrl.mp3Url + fileName);
            } else {
                player.setDataSource(fileName);
            }
            player.prepare();
            player.start();
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    if (isPlay) {
                        if (i == 1) {
                            playRing("luruchenggong.mp3");
                        } else {
                            playRing("wuxiao.mp3");
                        }
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause() {
        if (player.isPlaying()) {
            player.stop();
        }
        super.onPause();
    }

    @Override
    public void onDestroy() {
        player.release();
        super.onDestroy();
    }


    class ModelBean {
        String model;
        int num;

        public String getModel() {
            return model;
        }

        public int getNum() {
            return num;
        }

        public void setModel(String model) {
            this.model = model;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }

}